package manager.com.ithei.edu.info.manager.dao;

import manager.com.ithei.edu.info.manager.domain.Student;
import manager.com.ithei.edu.info.manager.domain.Teacher;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;

public class OtherTeacherDao implements BaseTeacherDao{
    private static ArrayList<Teacher> listt = new ArrayList<Teacher>();

    //教师主界面
    static {
        System.out.println("- - - - - - - - - - loading...........");
        load01();
    }



    //查找老师
    @Override
    public Teacher[] findAllTeachers() {
        //保存数据到文件中去方法1
        reSave1();
        return listt.toArray(new Teacher[listt.size()]);
    }

    //增加老师
    @Override
    public boolean addTeacher(Teacher t) {
        listt.add(t);
        //保存数据到文件中去方法1
        reSave1();
        return true;
    }

    //获取学号对应对象的索引
    @Override
    public int getIndex(String id) {
        int index = -1;
        for (int i = 0; i < listt.size(); i++) {
            Teacher t = listt.get(i);
            if (t != null && t.getSid().equals(id)) {
                index = i;
                break;
            }
        }
        return index;
    }

    //删除教师的方法
    @Override
    public void deleteTeacherByIdTwo(String id) {
        int index = getIndex(id);
        listt.remove(index);
        //保存数据到文件中去方法1
        reSave1();
    }

    //设置老师对象
    @Override
    public void setTeacherByIdTwo(String id, Teacher teachers) {
        //通过学号查找集合中对应的索引值
        int index = getIndex(id);
        //集合添加元素
        listt.set(index, teachers);
        //保存数据到文件中去方法1
        reSave1();
    }

//将系统中最新的数据写出到文件,进行存档 --基于普通字节输出流
    public  static void reSave1(){
        try {
            //1.创建一个字节输出流对象
            FileOutputStream fos=new FileOutputStream("StudentManageSystem\\teacher.txt",true);
            //2.基于输出流对象将集合中的数据写出到文件
            listt.forEach(teacher->{try {fos.write(teacher.toString().getBytes());
                fos.write(System.lineSeparator().getBytes());
            } catch (IOException e) {
                e.printStackTrace();
            }
            });
            //3.关闭流,释放资源
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //将上次存档的数据从新读回内存---基于普通字节输入流
    private static void load01() {
        try {
            //1.创建字节输入流
            FileInputStream fis=new FileInputStream("StudentManageSystem\\teacher.txt");
            //2.循环读,将文件中所有的数据都读入内存
            byte[] bys=new byte[1024*1024];
            int len=fis.read(bys);
            //3.
            String allTeacher=new String(bys,0,len);
            //4.先通过换行符切割
            String[] split = allTeacher.split(System.lineSeparator());
            //5.先遍历拿到没一个学生的信息
            for (int i = 0; i < split.length; i++) {
                //再根据,对单个学生信息进行切割
                String[] ss=split[i].split(",");
                Teacher tea=new Teacher(ss[0],ss[1],ss[2],ss[3],ss[4]);
                listt.add(tea);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
